package com.avic.rong.activity.mine;

import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.avic.rong.BaseActivity;
import com.avic.rong.R;
import com.avic.rong.api.mine.UpIntroductionApi;
import com.avic.rong.questionOrAnswer.StringUtil;
import com.avic.rong.utils.SharedPreferencesUtil;
import com.avic.rong.utils.ConstantsUtils;
import com.avic.rong.utils.ToastTools;
import com.example.minato.minastore.BaseResultEntity;
import com.example.minato.minastore.http.HttpManager;
import com.example.minato.minastore.listener.HttpOnNextListener;
import com.luck.picture.lib.dialog.CustomDialog;

import butterknife.BindView;

//账号设置-个人信息-修改个人简介
public class UpInfoActivity extends BaseActivity {


    @BindView(R.id.edittext_info)
    EditText edittext_info;
    private String login_token;
    private String introduce;
    private Intent intent;
    @BindView(R.id.tv_total)
    TextView tvTotal;
    private String oldinfo;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_up_info;
    }

    @Override
    protected void initView() {
        tvRight.setText("确定");
        tvTitle.setText("修改简介");
        tvRight.setTextColor(getResources().getColor(R.color.white));

        if (SharedPreferencesUtil.isLogin()) {
            login_token = SharedPreferencesUtil.getLoginToken();
        }

        intent = getIntent();
        if (intent!=null) {
            oldinfo = intent.getStringExtra("info");
            if (!StringUtil.isEmpty(oldinfo)) {
                edittext_info.setText(oldinfo);
                tvTotal.setText(String.valueOf(20-oldinfo.length()));
            }
        }
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String trim = edittext_info.getText().toString().trim();
                if (trim.equals(oldinfo)){
                    finish();
                }else {
                    showDialog();
                }
            }
        });
    }

    private void showDialog() {
        CustomDialog dialog = new CustomDialog(this, R.layout.dialog_upname);
        dialog.show();
        dialog.setCanceledOnTouchOutside(false);
        dialog.findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.cancel();
            }
        });



        dialog.findViewById(R.id.tv_ok).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    @Override
    protected void addListener() {

        tvRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


        edittext_info.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Log.e("onTextChanged", "beforeTextChanged: start:" +start+"  count:"+count+"  before:"+before);
                int total = 20-(start+count);
                tvTotal.setText(String.valueOf(total));
                if (total>=0){
                    tvTotal.setTextColor(getResources().getColor(R.color.color_1e81d2));
                }else {
                    tvTotal.setTextColor(getResources().getColor(R.color.myred));
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                Log.e("onTextChanged", "s:"+s.length());
                        if ((20-s.length())<0){
                            tvRight.setClickable(false);
                            tvRight.setOnClickListener(null);
                        } else if (s.length() == 0){
                           finish();
                        }  else{
                            tvRight.setClickable(true);
                            tvRight.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    upIntroduction();
                                }
                            });
                        }
            }
        });
    }

    public static Intent queryIntent(Context context) {
        Intent intent = new Intent(context, UpInfoActivity.class);
        return intent;
    }


    private void upIntroduction() {
        introduce = edittext_info.getText().toString().trim();
        UpIntroductionApi upIntroductionApi = new UpIntroductionApi(this, upIntroduceListener);
        upIntroductionApi.setArgument(login_token, introduce);
        HttpManager.getInstance().doHttpDeal(upIntroductionApi);

    }

    HttpOnNextListener<BaseResultEntity> upIntroduceListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity baseResultEntity) {
            if (baseResultEntity.getCode().equals(ConstantsUtils.STATUS_SUCCESS_0000)){
                ToastTools.showPublicToast("修改成功");
               intent.putExtra("info_new",introduce);
               setResult(300,intent);
                finish();
            }else {
                Toast.makeText(UpInfoActivity.this, baseResultEntity.getMsg(), Toast.LENGTH_SHORT).show();
            }
        }
    };

}
